<template>
  <el-container class="home-container">
    <el-header>
      <div class="header_img">
        <img src="../assets/chouyan1.png" alt />
        <span>澳门皇冠系统</span>
      </div>
      <el-button class="header_button" type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <!-- 动态为侧边栏设置宽度 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          |||
        </div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu v-for="item in menuList" :index="item.path" :key="item.id" >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
            </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体结构 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  name: 'sdf',
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: 'el-icon-question',
        103: 'el-icon-s-help',
        101: 'el-icon-s-home',
        102: 'el-icon-sell',
        145: 'el-icon-service'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    // 获取菜单数据
    this.getMenuList()
    // 重新进入到这一个页面中，定位到上一次访问的位置。
    this.getActivePath()
  },
  methods: {
    // 退出登录按钮
    logout() {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        this.$message({
          message: res.meta.msg
        })
      }
      this.menuList = res.data
    },
    // 切换菜单的折叠效果
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 菜单栏高亮显示
    saveNavState(path) {
      console.log(path, 'path')
      sessionStorage.setItem('activePath', path)
      this.activePath = path
    },
    getActivePath() {
      this.activePath = sessionStorage.getItem('activePath')
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  position: relative;
  width: 100% !important;
  height: 100px !important;
  padding-left: 0;
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.header_img {
  margin-left: 20px;
  img {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    vertical-align: middle;
  }
  span {
    color: #fff;
    font-size: 24px;
    margin-left: 20px;
  }
}
.header_button {
}
.el-aside {
  height: 100%;
  background-color: #333744;
}
.el-main {
  height: 100%;
  background-color: #eaedf1;
}
// el-menu 让导航栏的右border 颜色为
.el-menu {
  border: #333744;
}
.toggle-button {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
}
</style>
